package com.cssw.fyzb.busi.api.task;

import com.cssw.fyzb.core.model.vo.task.TodayTaskRecordVo;
import com.cssw.fyzb.busi.service.task.TaskService;

import com.cssw.fyzb.core.model.dodb.RuleInst;
import com.cssw.fyzb.core.model.dodb.TaskExec;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Function(description = "今日任务记录", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
})
@Component
public class MyTaskRecord implements Action {

    private final static Logger logger = LoggerFactory.getLogger(MyTaskRecord.class);

    @Autowired
    private TaskService taskService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        try {
            Page<RuleInst> ruleInsts = taskService.myTaskRecord(userId, 1, 10);
            List<TodayTaskRecordVo> list = new ArrayList<>();
            //从mongodb 获取用户的视频观看条数
            Integer taskStatMo = taskService.getTaskStatMo(userId);

            TodayTaskRecordVo todayTaskRecordVo = null;
            for (RuleInst r : ruleInsts.getList()) {
                todayTaskRecordVo = new TodayTaskRecordVo();
                todayTaskRecordVo.setTaskId(r.getTaskId());
                todayTaskRecordVo.setUserId(r.getUserId());
                todayTaskRecordVo.setInstId(r.getInstId());
                todayTaskRecordVo.setTaskName(r.getTaskName());
                TaskExec taskExec = taskService.getTaskExec(userId, r.getInstId());
                if (taskExec != null) { //有值就是完成
                    taskStatMo = taskStatMo - Integer.valueOf(r.getTaskTime());
                    todayTaskRecordVo.setCompletion("100");
                    todayTaskRecordVo.setTime(Integer.getInteger(r.getTaskTime()));
                } else if (0 == taskStatMo) {
                    todayTaskRecordVo.setCompletion("0");
                    todayTaskRecordVo.setTime(0);
                } else {//没有执行记录 求比例
                    //今日
                    if (taskStatMo > Integer.valueOf(r.getTaskTime())) {//判断观看条数是否大于
                        todayTaskRecordVo.setCompletion("100");
                        taskStatMo = taskStatMo - Integer.valueOf(r.getTaskTime());
                        todayTaskRecordVo.setTime(Integer.valueOf(r.getTaskTime()));
                    } else if (taskStatMo == Integer.valueOf(r.getTaskTime())) {
                        todayTaskRecordVo.setCompletion("100");
                        taskStatMo = taskStatMo - Integer.valueOf(r.getTaskTime());
                        todayTaskRecordVo.setTime(Integer.valueOf(r.getTaskTime()));
                    } else {
                        BigDecimal a = new BigDecimal(taskStatMo).divide((new BigDecimal(Integer.valueOf(r.getTaskTime()))), 2, BigDecimal.ROUND_HALF_EVEN);
                        todayTaskRecordVo.setCompletion((a.multiply(new BigDecimal(100))) + "");
                        todayTaskRecordVo.setTime(taskStatMo);
                        taskStatMo = 0;
                    }

                }
                list.add(todayTaskRecordVo);
            }
            return Response.success().put("list", list).put("total", ruleInsts.getTotalPage()).put("count", ruleInsts.getTotalRow());
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("查询失败");
        }
    }

}
